<!-- 序列号 -->
<template>
  <div>
    <el-card>
      <el-row>
        <el-col :span="8">
          序列号：
          <el-input
            placeholder="序列号"
            v-model="searchModel.serialNumber"
            style="width: 300px; margin: 10px"
            clearable
          ></el-input>
        </el-col>
        <el-col :span="8">
          商品名称：
          <el-select
            v-model="searchModel.productId"
            filterable
            placeholder="请选择"
            style="width: 300px; margin: 10px"
            clearable
          >
            <el-option
              v-for="item in goodsNames"
              :key="item.id"
              :label="item.goodsName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="8">
          仓库名称：
          <el-select
            v-model="searchModel.warehouseId"
            placeholder="仓库名称"
            style="width: 300px; margin: 10px"
            clearable
          >
            <el-option
              v-for="item in warehouseNames"
              :key="item.id"
              :label="item.warehouseName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getInventoryList()"
            >查询</el-button
          >
          <!-- <el-button type="primary" @click="InventoryUi(null)">新增</el-button> -->
        </el-col>
      </el-row>
    </el-card>
    <el-card>
      <el-table
        :data="inventoryList"
        border
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column prop="serialNumber" label="序列号" width="180">
          <template slot-scope="scope">
            <el-link
              type="text"
              class="btn-copy"
              :data-clipboard-text="scope.row.serialNumber"
              @click="copyMessage()"
              >{{ scope.row.serialNumber }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column prop="goodsName" label="商品名称" width="180">
        </el-table-column>
        <el-table-column prop="warehouseName" label="仓库" width="180">
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status">异常</el-tag>
            <el-tag v-else>正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="text" @click="InventoryUi(scope.row.id)"
              >编 辑</el-button
            >
            <el-button type="text" @click="deleteById(scope.row)"
              >删 除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 编辑仓库对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="520px"
      @close="cleaForm"
      :before-close="handleClose"
    >
      <el-form :model="inventoryForm" :rules="rules" ref="InventoryFormRef">
        <el-form-item
          label="序列号"
          :label-width="formLabelWidth"
          prop="serialNumber"
        >
          <el-input
            placeholder="序列号"
            v-model="inventoryForm.serialNumber"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="商品名称"
          :label-width="formLabelWidth"
          prop="productId"
          v-if="isAddInventory"
        >
          <el-select
            v-model="inventoryForm.productId"
            filterable
            placeholder="商品名称"
            clearable
          >
            <el-option
              v-for="item in goodsNames"
              :key="item.id"
              :label="item.goodsName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="仓库名称"
          :label-width="formLabelWidth"
          prop="warehouseId"
          v-if="isAddInventory"
        >
          <el-select
            filterable
            v-model="inventoryForm.warehouseId"
            placeholder="仓库名称"
            clearable
          >
            <el-option
              v-for="item in warehouseNames"
              :key="item.id"
              :label="item.warehouseName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="正常/异常"
          prop="status"
          :label-width="formLabelWidth"
        >
          <el-switch v-model="inventoryForm.status"> </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveInventory()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import warehouseApi from "@/api/inventory/warehouse";
import productApi from "@/api/inventory/product";
import inventoryApi from "@/api/inventory/inventory";
import { getToken } from "@/utils/auth";
import ClipboardJS from "clipboard"; // 复制

export default {
  mounted() {
    new ClipboardJS(".btn-copy"); // 复制文本
  },
  data() {
    return {
      loading: false,

      title: "",
      total: 0,
      dialogFormVisible: false,

      formLabelWidth: "90px",

      // 库存列表
      inventoryList: [],

      inventoryForm: {
        status: false,
      },

      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },

      // 仓库名称
      warehouseNames: [],
      // 商品名称
      goodsNames: [],

      rules: {
        serialNumber: [
          { required: true, message: "请输入序列号", trigger: "blur" },
          {
            min: 1,
            max: 40,
            message: "长度在 1 到 40 个字符",
            trigger: "blur",
          },
        ],
        warehouseId: [
          { required: true, message: "请选择分类", trigger: "change" },
        ],
        productId: [
          { required: true, message: "请选择分类", trigger: "change" },
        ],
      },
      isAddInventory: false,
    };
  },
  mounted() {},
  methods: {
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getInventoryList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getInventoryList();
    },
    // 确认关闭对话框
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },

    // 清理表单数据
    cleaForm() {
      if (
        this.$refs.InventoryFormRef != null &&
        this.$refs.InventoryFormRef != undefined
      ) {
        this.$refs.InventoryFormRef.clearValidate();
      }
      // 清理数据
      this.inventoryForm = {
        status: false,
      };
    },

    // 获取所有商品名称，和仓库名称
    getAllSelectName() {
      warehouseApi.selseAllName().then((response) => {
        this.warehouseNames = response.data;
      });
      productApi.selseAllName().then((response) => {
        this.goodsNames = response.data;
      });
    },

    // 保存序列号
    saveInventory() {
      this.$refs.InventoryFormRef.validate((valid) => {
        if (valid) {
          // 提交数据给后台
          inventoryApi.saveInventory(this.inventoryForm).then((response) => {
            if (response.code === 20000) {
              this.$message({
                showClose: true,
                message: response.message,
                type: "success",
              });
              // 关闭对话框
              this.dialogFormVisible = false;
              // 刷新表单
              this.getInventoryList();
            } else {
              this.$message({
                showClose: true,
                message: response.message,
                type: "error",
              });
              this.getInventoryList();
            }
          });
        } else {
          // 验证不通过
          this.$message({
            showClose: true,
            message: "表单验证不通过",
            type: "error",
          });
          return false;
        }
      });
    },

    InventoryUi(id) {
      if (id == null || id == undefined) {
        this.isAddInventory = true;
        this.title = "新增序列号";
      } else {
        this.title = "编辑序列号";
        this.isAddInventory = false;

        inventoryApi.getInventoryById(id).then((response) => {
          this.inventoryForm = response.data;
        });
      }
      this.dialogFormVisible = true;
    },

    getInventoryList() {
      this.loading = true;
      inventoryApi.getInventoryList(this.searchModel).then((response) => {
        this.inventoryList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    // 删除库存
    deleteById(inventory) {
      // 判断序列号列表是否为空
      this.$confirm(
        `序列号${inventory.serialNumber}, 您确认要删除吗？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          inventoryApi
            .deleteInventoryById(inventory.id, getToken())
            .then((response) => {
              this.$message({
                type: "success",
                message: response.message,
              });
              this.getInventoryList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 点击复制提示
    copyMessage() {
      this.$message({
        type: "success",
        message: "复制成功",
      });
    },
  },

  created() {
    this.getAllSelectName();
    this.getInventoryList();
  },
};
</script>

<style scoped></style>
